package com.xyht.sca_s.student_manage_system.modules.org.util;

import com.xyht.sca_s.student_manage_system.modules.org.entity.OrgNode;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Component
public class NodeUtil {
     public  static  List<Integer> returnList = new ArrayList<Integer>();
    /**
     * 根据父节点的ID获取所有子节点
     * @param list 分类表
     * @param typeId 传入的父节点ID
     * @return String
     */
    public static List<Integer>  getChildNodes(List<OrgNode> list, Integer typeId) {
        returnList = new ArrayList<Integer>();
        if(list == null && typeId == null) return null;
        assert list != null;
        for (OrgNode node : list) {
            // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
//            if (node.getParentId() == 0 && typeId.equals(node.getId())) {
//                recursionFn(list, node);
//            }
            if (typeId.equals(node.getId())) {
                recursionFn(list, node);
            }
            // 二、遍历所有的父节点下的所有子节点
            /*if (node.getParentId()==0) {
                recursionFn(list, node);
            }*/
        }
        return returnList;
    }

    private static void recursionFn(List<OrgNode> list, OrgNode node) {
        List<OrgNode> childList = getChildList(list, node);// 得到子节点列表
        if (hasChild(list, node)) {// 判断是否有子节点
            returnList.add(node.getId());
            Iterator<OrgNode> it = childList.iterator();
            while (it.hasNext()) {
                OrgNode n = (OrgNode) it.next();
                recursionFn(list, n);
            }
        } else {
            returnList.add(node.getId());
        }
    }

    // 得到子节点列表
    private static List<OrgNode> getChildList(List<OrgNode> list, OrgNode node) {
        List<OrgNode> nodeList = new ArrayList<OrgNode>();
        Iterator<OrgNode> it = list.iterator();
        while (it.hasNext()) {
            OrgNode n = (OrgNode) it.next();
            if (n.getParentId().equals(node.getId())) {
                nodeList.add(n);
            }
        }
        return nodeList;
    }

    // 判断是否有子节点
    private static boolean hasChild(List<OrgNode> list, OrgNode node) {
        return getChildList(list, node).size() > 0 ? true : false;
    }

    public static List<Integer> getParentsByLevel(List<OrgNode> list, Integer childId, int targetLevel) {
        List<Integer> parentIds = new ArrayList<>();
        if (list == null || childId == null) return null;

        OrgNode currentNode = findNodeById(list, childId);

        if (currentNode == null) return null;

        while (currentNode != null && Integer.parseInt(currentNode.getLevel()) >= targetLevel) {
            if (Integer.parseInt(currentNode.getLevel()) == targetLevel) {
                parentIds.add(currentNode.getId());
            }
            currentNode = findParentNode(list, currentNode);
        }

        return parentIds;
    }

    private static OrgNode findNodeById(List<OrgNode> list, Integer id) {
        for (OrgNode node : list) {
            if (node.getId().equals(id)) {
                return node;
            }
        }
        return null;
    }

    private static OrgNode findParentNode(List<OrgNode> list, OrgNode currentNode) {
        for (OrgNode node : list) {
            if (node.getId().equals(currentNode.getParentId())) {
                return node;
            }
        }
        return null;
    }

}
